package com.yaorange.jk.web.cargo;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Export;
import com.yaorange.jk.entity.ExportProduct;
import com.yaorange.jk.service.ContractService;
import com.yaorange.jk.service.ExportService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @author coach tam
 * @date 2017/11/1
 */
public class ExportAction extends BaseAction implements ModelDriven<Export> {

    /**
     * 因为模型驱动需要实现getModel，然后我们把这个对象传递给jsp 需要单独弄一个get方法。
     * 所以，我们把名字改成model，一个getModel 就搞定了。
     */
    private Export model = new Export();

    //分页对象
    private Pagination page = new Pagination();

    @Autowired
    private ExportService exportService;

    @Autowired
    private ContractService contractService;


    private String[] mr_id;
    private String[] mr_changed;
    private Integer[] mr_orderNo;
    private Integer[] mr_cnumber;
    private Double[] mr_grossWeight;
    private Double[] mr_netWeight;
    private Double[] mr_sizeLength;
    private Double[] mr_sizeWidth;
    private Double[] mr_sizeHeight;
    private Double[] mr_exPrice;
    private Double[] mr_tax;
    /**
     * 查询待报运合同
     */
    public String contractList(){
        page = contractService.findPage(page.getPageNo(),page.getPageSize(), 1);
        page.setUrl("exportAction_contractList");
        push(page);
        return "contractList";
    }

    /**
     * 分页查询
     */
    public String list() {
        page = exportService.findPage(page.getPageNo(), page.getPageSize());
        page.setUrl("exportAction_list");
        push(page);
        return "list";
    }

    /**
     * 对报运单进行报上报 ---》装箱管理 (将报运单表export_c 的state 字段更新为1)
     * private Integer state;				//0-草稿 1-已上报
     * @return
     */
    public String submit(){
        exportService.submit(model.getId());
        return "rlist";
    }

    public String cancel(){
        //取消ids:4028813d5fc3f6a0015fc41d21dc0000, 4028813d5fa48a90015fa494f1910000
        System.out.println("取消ids:"+model.getId());
        exportService.cancel(model.getId());
        return "rlist";
    }


    /**
     * 报运
     */
    public String tocreate() {

        return "tocreate";
    }

    /**
     * 添加 保存（1或n个）购销合同--》报运表中
     */
    public String insert() {
        exportService.save(model);
        return contractList();
    }

    /**
     * 跳转更新页面
     * @return
     */
    public String toupdate() {
        //根据id查询部门老数据
        model = exportService.findById(model.getId());
        push(model);
        return "toupdate";
    }

    public String getExportProduct() throws IOException {
        Export exp = exportService.findById(model.getId());
        Set<ExportProduct> products = exp.getExportProducts();
        String proJson = JSON.toJSONString(products, true);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(proJson);
        return NONE;
    }
    /**
     *更新
     */
    public String update() {
        exportService.update(model,mr_id,mr_changed,mr_orderNo,mr_cnumber,mr_grossWeight,mr_netWeight,mr_sizeLength,mr_sizeWidth,mr_sizeHeight,mr_exPrice,mr_tax);
        return "rlist";
    }

    /**
     * 批量删除
     */
    public String delete() {
        String[] ids = model.getId().split(", ");
        exportService.deleteByIds(ids);
        return "rlist";
    }



    public void setPage(Pagination page) {
        this.page = page;
    }

    public Pagination getPage() {
        return page;
    }

    @Override
    public Export getModel() {
        return model;
    }

    public void setMr_id(String[] mr_id) {
        this.mr_id = mr_id;
    }

    public void setMr_changed(String[] mr_changed) {
        this.mr_changed = mr_changed;
    }

    public void setMr_orderNo(Integer[] mr_orderNo) {
        this.mr_orderNo = mr_orderNo;
    }

    public void setMr_cnumber(Integer[] mr_cnumber) {
        this.mr_cnumber = mr_cnumber;
    }

    public void setMr_grossWeight(Double[] mr_grossWeight) {
        this.mr_grossWeight = mr_grossWeight;
    }

    public void setMr_netWeight(Double[] mr_netWeight) {
        this.mr_netWeight = mr_netWeight;
    }

    public void setMr_sizeLength(Double[] mr_sizeLength) {
        this.mr_sizeLength = mr_sizeLength;
    }

    public void setMr_sizeWidth(Double[] mr_sizeWidth) {
        this.mr_sizeWidth = mr_sizeWidth;
    }

    public void setMr_sizeHeight(Double[] mr_sizeHeight) {
        this.mr_sizeHeight = mr_sizeHeight;
    }

    public void setMr_exPrice(Double[] mr_exPrice) {
        this.mr_exPrice = mr_exPrice;
    }

    public void setMr_tax(Double[] mr_tax) {
        this.mr_tax = mr_tax;
    }
}
